package com.igeek.shop.web.houtai;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.igeek.shop.entity.Order;
import com.igeek.shop.service.ProductService;
import com.igeek.shop.service.ProductServiceImpl;
import com.igeek.shop.service.ProductServicees;

public class OrderServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private  ProductServicees ps = new ProductServiceImpl();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String state = req.getParameter("state");
		if(state.equals("findAllOrders")){
			findAllOrders(req,resp);
		}else if(state.equals("findOrderInfoByOid")){
			findOrderInfoByOid(req,resp);
		}
	}

	private void findOrderInfoByOid(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		//休眠
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//获取订单编号
		String oid=req.getParameter("oid");
		
		//面向接口编程
		//AdminService service = new AdminServiceImpl();
		//bean---名称---对应一个类名  --写到配置文件中
		List<Map<String,Object>> mapList = ps.findOrderInfoByOid(oid);
		
		//ajax请求通过response写回去
		Gson gson = new Gson();
		String json = gson.toJson(mapList);
		//[
		//{"shop_price":2299.0,"count":2,"pname":"宏碁（acer）ATC705-N50 台式电脑","pimage":"products/1/c_0031.jpg","subtotal":4598.0},
		//{"shop_price":1299.0,"count":4,"pname":"小米 4c 标准版","pimage":"products/1/c_0001.jpg","subtotal":5196.0},
		//{"shop_price":2298.0,"count":1,"pname":"vivo X5Pro","pimage":"products/1/c_0014.jpg","subtotal":2298.0}
		//]

		//System.out.println(json);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(json);
	}

	private void findAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Order> orderList = ps.findAllOrders();
		req.setAttribute("orderList", orderList);
		req.getRequestDispatcher("/admin/order/list.jsp").forward(req, resp);
	}
	
	

}
